package com.vital.mshopping;

import java.util.ArrayList;

import com.vital.dao.Category;
import com.vital.dao.History;
import com.vital.service.DatabaseService;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 * 	@author btan
 *	@email: btan@tma.com.vn - anbtan@gmail.com
 */
public class HistoryActivity extends Activity {
	ListView lvHistory;
	ArrayList<History> histories;
	HistoryAdapter adapterHistory;
	Category category;
	Context mContext;
	int posSelectedItem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		
		lvHistory = (ListView)findViewById(R.id.lvHistory);
		mContext = this.getApplicationContext();
		histories = new ArrayList<History>();
		
		
		new LoadAllHistories().execute();
	}

	
	public void setAdapterListHistories() {
		adapterHistory = new HistoryAdapter(mContext, R.layout.item_location, histories);
		lvHistory.setOnItemClickListener(onItemClick);
		lvHistory.setOnItemLongClickListener(onItemLongClick);
		lvHistory.setAdapter(adapterHistory);
	}
	
	private OnItemClickListener onItemClick = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			showHistoryDetail(position);
		}
	};
			
	private OnItemLongClickListener onItemLongClick = new OnItemLongClickListener() {

		public boolean onItemLongClick(AdapterView<?> parent, View view,
				int position, long id) {
			// TODO Auto-generated method stub
			//---Show option menu with three item: delete/view detail/view map---
			showOptionMenu(position);
			return true;
		}
	};
			
	/**
	 * show option menu with two options
	 * 1. Edit category
	 * 2. Delete category
	 */
	private void showOptionMenu(int position) {
		posSelectedItem = position;
		AlertDialog.Builder builder = new Builder(HistoryActivity.this);
		builder.setTitle(R.string.bookmark_dialog_menu_title);
		builder.setItems(R.array.bookmark_dialog_menu_array_choose, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch (which) {
					case 0:
						// Delete
						deleteHistory(posSelectedItem);
						break;
					case 1:
						// Map view
						showHistoryOnMap(posSelectedItem);
						break;
					case 2:
						// Detail view
						showHistoryDetail(posSelectedItem);
						break;
				}
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	};
	
	private void deleteHistory(int position) {
		DatabaseService db = new DatabaseService(mContext);
		db.open();
		if(db.removeHistory(histories.get(position).getId())) {
			histories.remove(position);
			adapterHistory.notifyDataSetChanged();
		}
		db.close();
	}
	
	private void showHistoryOnMap(int position) {
		History history = histories.get(position);
		
		Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
		intent.putExtra("ObjHistory", history);
		startActivity(intent);
	}
	
	private void showHistoryDetail(int position) {
		History history = histories.get(position);
		
		Intent intent = new Intent(getApplicationContext(), DetailLocationActivity.class);
		intent.putExtra("ObjHistory", history);
		startActivity(intent);
	}
	
	
	public void loadAllHistoriesFromDB() {
		DatabaseService db = new DatabaseService(mContext);
		db.open();
		Cursor c = db.getAllHistories();
		if (c.moveToFirst()) {
			do {
				//---set value---
				History history = new History();
				history.setLocationId(c.getInt(0));
				history.setLat(c.getFloat(1));
				history.setLng(c.getFloat(2));
				history.setName(c.getString(3));
				history.setPhone(c.getString(4));
				history.setAddress(c.getString(5));
				history.setCategory(c.getInt(6));
				history.setId(c.getInt(7));
				history.setHisDate(c.getString(8));
				history.setAccuracy(c.getString(9));
				 
				//---add it to category list
				histories.add(history);
			} while (c.moveToNext());
		}
		c.close();
		db.close();
	}
	
	private class LoadAllHistories extends AsyncTask<String, Void, String> {
		
		private final ProgressDialog progress = new ProgressDialog(HistoryActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress.setMessage("Loading...");
			progress.show();
		}
		
		
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			loadAllHistoriesFromDB();
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			//---Set adapter for list view category---
	        setAdapterListHistories();
			if (progress.isShowing()) {
				progress.dismiss();
			}
			 
		}
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_history, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.history_menu_deleteAll:
			deleteAllHistory();
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/**
	 * delete all history
	 */
	private void deleteAllHistory() {
		DatabaseService db = new DatabaseService(mContext);
		db.open();
		db.removeAllHistories();
		db.close();
	}
}
